class_name UnitVampire
extends UnitBase


@export var born_unit_data: UnitBaseData
var bats: Array[UnitBase] = []
var is_in_bat_state: bool = false
@export var max_summon_count: int = 20
var summon_interval_time: float = 0.1 
var summon_cooldown_time: float = 5 
var summon_cooldown_timer: float = 5
var bat_state_duration: float = 30
var recover_valid_distance: float = 48


func pick_new_wander_target():
	# 在矩形范围内随机目标点
	var x_offset = randf_range(-wander_width/2, wander_width/2)
	var y_offset = randf_range(-wander_height/2, wander_height/2)
	target_pos = home_position + Vector2(x_offset, y_offset)
	
	# 下一次切换目标时间
	timer = randf_range(1.0, change_dir_time)
	
	
func play_wander_animation():
	animation_player.play("idle")
	

func process_enter_summon(delta: float):
	if not attack_target: return
	
	summon_cooldown_timer -= delta
	if summon_cooldown_timer <= 0:
		summon_cooldown_timer = summon_cooldown_time
		states.transition("Summon")


func born_unit():
	var unit_data = born_unit_data.duplicate(true) as UnitBaseData
	unit_data.is_enemy = self.is_enemy
	unit_data.home_position = ray_cast_point.global_position
	unit_data.pos = ray_cast_point.global_position + Vector2(randf_range(-1, 1), randf_range(-1, 1)).normalized() * 8
	
	var level = get_tree().current_scene as Level
	level.level_data.units_data.append(unit_data)
	
	return level.create_unit_by_unit_data(unit_data) 


func summon_bats():
	bats = []
	attack_target = null
	
	for index in range(max_summon_count):
		if is_dead: break
		
		var unit_inst = born_unit()
		unit_inst.set_search_radius(search_sense_range_shape.shape.radius)
		unit_inst.velocity = Vector2(randf_range(-1, 1), randf_range(-1, 1)).normalized() * 200
		bats.append(unit_inst)
		await get_tree().create_timer(summon_interval_time).timeout


func process_bat_state_enter_death():
	if not is_in_bat_state: return
	
	var bats_remain_alive_count = 0
	for bat in bats:
		if is_instance_valid(bat):
			bats_remain_alive_count += 1
	
	if bats_remain_alive_count <= 0:
		is_dead = true
		states.transition("Death")
		

func process_recover():
	if is_dead: return
	
	var is_all_bats_returned: bool = true
	
	for bat in bats:
		if is_instance_valid(bat):
			var distance = bat.ray_cast_point.global_position.distance_to(ray_cast_point.global_position)
			if distance > recover_valid_distance:
				is_all_bats_returned = false
				break
	
	if is_all_bats_returned:
		is_in_bat_state = false
		
		for bat in bats:
			if is_instance_valid(bat):
				bat.death(false)
		bats = []
		
		var tween_alpha = create_tween()
		tween_alpha.tween_property(view_root, "modulate:a", 1.0, 0.3) \
		.set_trans(Tween.TRANS_SINE) \
		.set_ease(Tween.EASE_IN_OUT).from_current()
		
		enable_body_collision()
		enable_hurt_collision()
		enable_hit_collision()
		states.transition("Approach")
	

func all_bats_recover():
	for bat in bats:
		if is_instance_valid(bat) and not bat.is_dead:
			bat.set_move_2(ray_cast_point)
		

func set_is_enemy(is_enemy: bool):
	super.set_is_enemy(is_enemy)


func enable_physics():
	if not is_in_bat_state:
		super.enable_physics()
